package org.come.action.petpaixu;

import com.tool.role.RoleLingFa;
import com.tool.time.Limit;
import java.awt.image.ImageObserver;
import java.math.BigDecimal;
import com.tool.time.TimeLimit;
import com.tool.role.RoleData;
import java.awt.Font;
import javax.swing.ImageIcon;

import com.updateNew.MyIsif;
import org.come.Frame.TestSetupJframe;
import java.awt.RenderingHints;
import java.awt.Graphics2D;
import java.awt.Graphics;
import java.util.Map;
import come.tool.Fighting.Fightingimage;
import java.util.Iterator;
import come.tool.Fighting.TypeState;
import org.come.entity.RoleSummoning;
import org.come.model.Lingbao;
import org.come.until.UserMessUntil;
import java.util.List;
import java.util.HashMap;
import come.tool.Fighting.FightingMixDeal;
import com.tool.image.ImageMixDeal;
import javax.swing.JLabel;
import javax.swing.Box;
import java.awt.LayoutManager;
import java.awt.FlowLayout;
import java.awt.Color;
import java.awt.Component;
import javax.swing.JList;
import java.awt.Dimension;
import javax.swing.DefaultListModel;
import javax.swing.ListCellRenderer;
import javax.swing.JPanel;

public class MyRenderer extends JPanel implements ListCellRenderer {
    private int index;
    public static String vs;
    private static DefaultListModel<String> listModel;
    private Color color = Color.WHITE;

    public MyRenderer() {
        this(67); // 默认构造函数传入默认索引
    }

    public MyRenderer(final int index) {
        this.index = 67;
        this.index = index;
    }

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(getWidth() / 2, 22);
    }

    @Override
    public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
        final MyRenderer mr = new MyRenderer(index);
        mr.removeAll();
        if ( isSelected) {
            mr.setBackground(Color.gray);
        }
        else {
            mr.setOpaque(false);
        }

        Box box = Box.createHorizontalBox();
        JLabel iconLabel = new JLabel();
        iconLabel.setText("");

        try {
            if (ImageMixDeal.userimg.getRoleShow().getFighting() != 0) {
                iconLabel.setForeground(Color.BLACK);

                Fightingimage fightingimage = FightingMixDeal.getdata(0);
                if (fightingimage != null) {
                    List<TypeState> data = fightingimage.getFightingManData().cxxx("召唤兽");
                    int sid = -1;
                    if (UserMessUntil.getChosePetMes() != null) {
                        sid = UserMessUntil.getChosePetMes().getSid().intValue();
                    }

                    for (TypeState datum : data) {
                        if (sid == Integer.parseInt(datum.getType())) {
                            iconLabel.setForeground(Color.WHITE);
                        }
                    }
                }
            }
        } catch (Exception ignored) {}

        box.add(iconLabel);
        mr.add(box);
        return mr;
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);

        Graphics2D g2d = (Graphics2D) g;
        g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);

        List<RoleSummoning> petList = UserMessUntil.getPetListTable();

        // ✅ 安全检查：避免空指针或越界
        if (petList == null || index < 0 || index >= petList.size()) {
            return;
        }

        RoleSummoning roleSummoning = petList.get(index);
        String style = MyIsif.getStyle();

        // 绘制序号图标
        ImageIcon peiimg;
        if ("水墨UI".equals(style)) {
            peiimg = new ImageIcon("inkImg/button/ss507.png");
        } else {
            peiimg = new ImageIcon("inkimg/hongmu/ss544.png");
        }
        g.drawImage(peiimg.getImage(), 1, 3, 17, 17, null);

        // 设置字体样式
        Font fontNum = new Font("宋体", Font.PLAIN, 13);
        g2d.setFont(fontNum);

        // 设置颜色
        g2d.setColor("水墨UI".equals(style) ? new Color(255, 255, 255) : new Color(187, 165, 75));

        // 绘制编号
        String numberText = String.valueOf(index + 1);
        int x = index >= 9 ? 2 : 5;
        int y = 15;
        g2d.drawString(numberText, x, y);

        // 获取战斗数据
        final Fightingimage fightingimage = FightingMixDeal.getdata(0);
        color = Color.WHITE;

        if (fightingimage != null) {
            List<TypeState> data = fightingimage.getFightingManData().cxxx("召唤兽");
            for (TypeState datum : data) {
                if (roleSummoning.getSid().toString().equals(datum.getType())) {
                    if (datum.getState() == 2) {
                        color = Color.RED;
                    }
                    break;
                }
            }
        }

        // 绘制宠物名称
        Font fontName = new Font("宋体", Font.PLAIN, 17);
        g2d.setFont(fontName);
        g2d.setColor(color);

        if (RoleData.getRoleData().getLoginResult().getSummoning_id() != null &&
                RoleData.getRoleData().getLoginResult().getSummoning_id().compareTo(roleSummoning.getSid()) == 0) {
            g2d.setColor(new Color(187, 165, 75));
        }

        g2d.drawString(roleSummoning.getSummoningname(), 18, 17);

        // 枯荣丹图标绘制
        if (TimeLimit.getLimits().getLimit("枯荣丹") != null &&
                RoleData.getRoleData().getLoginResult().getSummoning_id() != null) {
            Limit limit = TimeLimit.getLimits().getLimit("枯荣丹");
            if (roleSummoning.getSid().compareTo(new BigDecimal(limit.getValue())) == 0) {
                ImageIcon iconem = new ImageIcon("inkimg/danxin/ss286.png");
                g.drawImage(iconem.getImage(), 124, 2, 19, 19, this);
            }
        }
    }
}